﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ZLCMS.Data.Model;
using NHibernate;
using DotNet.Common.NHibernateUtil;
using ZLCMS.Commands.User.Commands.Reconciliations;
using DotNet.Common.Command;
using ZLCMS.Business.DwzFrame;

namespace ZLCMS.Web.SystemManage.Basics.Shop.Reconciliations
{
    public partial class BillPaymentDetail : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {

                int uid = 0;
                IList<SysDepartmentInfo> deptList = null;

                Int32.TryParse(Request["uid"], out uid);

                ShopBillPayment item = null;
                using (NHibernate.ISession session = DotNet.Common.NHibernateUtil.SessionFactoryHelper.CreateSession())
                {
                    deptList = session.CreateQuery("from SysDepartmentInfo").List<SysDepartmentInfo>();
                    dept.DataSource = deptList;
                    dept.DataValueField = "DeptCode";
                    dept.DataTextField = "DeptName";
                    dept.DataBind();

                    if (uid > 0)
                    {
                        item = session.Get<ShopBillPayment>(uid);
                    }
                    if (item != null)
                    {
                        //保存uid
                        ViewState["uid"] = uid;
                    }
                    else
                    {
                        item = new ShopBillPayment();
                        item.BillWeeklyNo = Request["bwno"];
                        item.DeptCode = Request["dc"];
                        item.OptDate = DateTime.Now;
                        item.Status = "1";
                    }
                }
                //在此设置控件的初始属性

                tb_bwNo.Text = item.BillWeeklyNo;
                dept.SelectedValue = item.DeptCode;
                tb_amount.Text = string.Format("{0:0.00}", item.PaidFee);
                tb_memo.Text = item.Memo;

                if (!string.IsNullOrEmpty(item.BillWeeklyNo))
                {
                    tb_bwNo.ReadOnly = true;
                }
                if (!string.IsNullOrEmpty(item.DeptCode))
                {
                    dept.Enabled = false;
                }
            }
            else
            {
                //todo 保存数据
                EditBillPaymentParameter param = new EditBillPaymentParameter();
                int uid = ViewState["uid"] == null ? 0 : (int)ViewState["uid"];

                param.BillWeeklyNo = tb_bwNo.Text;
                param.DeptCode = dept.SelectedValue;
                param.Memo = tb_memo.Text;
                param.PayAmount = Convert.ToDecimal(tb_amount.Text);

                ICommandResult<EditBillPaymentResult> result = new EditBillPaymentCommand().Execute(param);

                FrameHelper _framePage = new FrameHelper(this);
                ReturnCode rc = new ReturnCode()
                {
                    statusCode = result.Error ? 300 : 200,
                    message = result.ErrorMsg
                };

                rc.callbackType = "forward";
                rc.forwardUrl = "/SystemManage/Basics/Shop/Reconciliations/BillPaymentDetail.aspx?tabid=BillPaymentDetail";

                if (Request["submit_op"] != "1" && !result.Error)
                {
                    rc.forwardUrl += "&uid=" + result.Result.Data.Id;
                }
                _framePage.Alert(rc);
            }

        }

    }
}